package com.csnf.util;

import com.csnf.dao.RoleDao2;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    private static InputStream inputStream;
    private static SqlSessionFactoryBuilder builder;
    private static SqlSessionFactory factory;
    private static SqlSession session;
    public static SqlSession init(){
        try {
            //读取mybatis-config.xml配置文件
            inputStream= Resources.getResourceAsStream("mybatis-config.xml");
            //创建SqlSessionFactory对象
            builder=new SqlSessionFactoryBuilder();
            factory=builder.build(inputStream);
            //创建SqlSession对象
            session=factory.openSession(true);//数据库增删改可以自动commit
            //创建RoleDao2的实例
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return session;
    }
    public static void close(){
        session.close();
    }
}
